package itbour.onetouchshow.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import itbour.onetouchshow.R;
import itbour.onetouchshow.activity.version1_3.MainActivityV3;
import itbour.onetouchshow.base.mvp.MVPBaseActivity;
import itbour.onetouchshow.utils.SpUtils;
import itbour.onetouchshow.widget.BootDialog;

/**
 * Created by Yang on 2017/12/16.
 * 引导页
 */

public class BootPageActivity extends MVPBaseActivity {


    @BindView(R.id.id_container)
    LinearLayout idContainer;
    @BindView(R.id.id_ig_skipped)
    ImageView idIgSkipped;
    LinearLayout.LayoutParams layoutParams;
    BootDialog bootDialog;
    private int[] bootImage = new int[]{R.mipmap.boot_one, R.mipmap.boot_two
            , R.mipmap.boot_three, R.mipmap.boot_four, R.mipmap.boot_over};
    private List<ImageView> imageViews = new ArrayList<>();
    private int currentPosition;

    @Override
    public void loadSucceed(String result) {

    }

    @Override
    public void loadFaild(String error) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_boot_page;
    }

    @Override
    protected void afterCreate(Bundle savedInstanceState) {
        SpUtils.getInstance().saveBoolean("isFirst", true);
        layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                , ViewGroup.LayoutParams.MATCH_PARENT);

        for (int i = 0; i < bootImage.length; i++) {
            ImageView imageView = new ImageView(getApplication());
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageView.setImageResource(bootImage[i]);
            imageView.setId(i + 1);
            imageView.setBackgroundColor(Color.BLACK);
            imageViews.add(imageView);
        }

        for (int i = 0; i < imageViews.size(); i++) {
            int finalI = i;
            imageViews.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (finalI == imageViews.size() - 1) {

                    } else {
                        idIgSkipped.setVisibility(View.VISIBLE);
                        currentPosition = finalI;
                        idContainer.removeAllViews();
                        idContainer.addView(imageViews.get(finalI + 1), layoutParams);
                        if (currentPosition == imageViews.size() - 2) {
                            idIgSkipped.setVisibility(View.GONE);
                            if (bootDialog == null) {
                                bootDialog = new BootDialog();
                            }
                            bootDialog.builder(BootPageActivity.this);
                            bootDialog.setOnListener(new BootDialog.BootOnListener() {
                                @Override
                                public void onTryagain() {
                                    bootDialog.dismiss();
                                    idContainer.removeAllViews();
                                    idContainer.addView(imageViews.get(0), layoutParams);
                                }

                                @Override
                                public void onMakeOver() {
                                    bootDialog.dismiss();
                                    openActivity(MainActivityV3.class);
                                    finish();
                                }
                            });
                        }
                    }
                }
            });
        }
        currentPosition = 0;
        idContainer.addView(imageViews.get(0), layoutParams);

        idIgSkipped.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openActivity(MainActivityV3.class);
                finish();
            }
        });
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            idIgSkipped.setVisibility(View.VISIBLE);
            if (currentPosition > 0) {
                idContainer.removeAllViews();
                idContainer.addView(imageViews.get(currentPosition), layoutParams);
                currentPosition--;
            } else {
                idContainer.removeAllViews();
                idContainer.addView(imageViews.get(0), layoutParams);
            }
            return true;
        }
        return false;
    }
}
